package com.zero.service;

import com.zero.entity.Web;
import com.zero.mapper.WebMapper;
import com.zero.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WebService {
    @Autowired
    private WebMapper webMapper;

    /**
     * 获取网站信息
     * @return
     */
    public Result getWebInfo() {
        List<Web> result = webMapper.select();
        if(result == null || result.size() != 1)
            return Result.fail("获取不到网站信息");
        return Result.ok("获取网站信息成功", result.get(0));
    }

    /**
     * 更新网站访问量
     * @return
     */
    public boolean updateWebView(){
        Web web = webMapper.selectById(1);
        web.setViews(web.getViews() + 1);
        int row = webMapper.update(web);
        if (row == 1)
            return true;
        return false;
    }

    /**
     * 更新用户数
     * @param type 0代表增加，1代表减少
     * @return
     */
    public boolean updateWebUserCount(int type){
        Web web = webMapper.selectById(1);
        if (type == 0)
            web.setUserCount(web.getUserCount() + 1);
        else
            web.setUserCount(web.getUserCount() - 1);
        int row = webMapper.update(web);
        if (row == 1)
            return true;
        return false;
    }

    /**
     * 增加家庭数和用户数
     * @return
     */
    public boolean updateWebCount(){
        Web web = webMapper.selectById(1);
        web.setFamilyCount(web.getFamilyCount() + 1);
        web.setUserCount(web.getUserCount() + 1);
        int row = webMapper.update(web);
        if (row == 1)
            return true;
        return false;
    }

    /**
     * 更新网站信息
     * @param web
     * @return
     */
    public Result updateWebInfo(Web web) {
        int row = webMapper.update(web);
        if (row == 1)
            return Result.ok("更新成功");
        return Result.fail("更新失败");
    }
}
